package com.itfreer.ui.base.datetime;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TimePicker;

import com.itfreer.ui.R;

/**
 * 自定义时间控件器
 */
public class TimePickerDialog extends AlertDialog implements DialogInterface.OnClickListener, TimePicker.OnTimeChangedListener {

    private final TimePicker mTimePicker;
    private final OnTimeSetListener mCallBack;

    public interface OnTimeSetListener {
        void onDateSet(TimePicker timePicker, int hourOfDay, int minute);
    }

    public TimePickerDialog(Context context, int theme, OnTimeSetListener callBack) {
        this(context, theme, callBack, 0, 0);
    }

    public TimePickerDialog(Context context, int theme, OnTimeSetListener callBack, int hourOfDay, int minute) {
        super(context, theme);

        mCallBack = callBack;
        Context themeContext = getContext();
        setButton(BUTTON_POSITIVE, "确 定", this);
        setButton(BUTTON_NEGATIVE, "取 消", this);
        setIcon(0);

        LayoutInflater inflater = (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.com_itfreer_core_ui_dialog_timepickerdialog, null);
        setView(view);
        mTimePicker = (TimePicker) view.findViewById(R.id.datePicker);
        mTimePicker.setIs24HourView(true);

        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minute);
    }

    public void onClick(DialogInterface dialog, int which) {
        // 如果是“取 消”按钮，则返回，如果是“确 定”按钮，则往下执行
        if (which == BUTTON_POSITIVE){
            tryNotifyDateSet();
        }
    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        if (view.getId() == R.id.datePicker){
            mTimePicker.setCurrentHour(hourOfDay);
            mTimePicker.setCurrentMinute(minute);
        }
    }

    /**
     * 获得开始日期的DatePicker
     *
     * @return The calendar view.
     */
    public TimePicker getDatePicker() {
        return mTimePicker;
    }

    public void updateDate(int hourOfDay, int minute) {
        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minute);
    }

    private void tryNotifyDateSet() {
        if (mCallBack != null) {
            mTimePicker.clearFocus();
            mCallBack.onDateSet(mTimePicker, mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private static String START_HOUR = "ITMEPICKER_START_HOUR";
    private static String START_MINUTE = "ITMEPICKER_START_MINUTE";

    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(START_HOUR, mTimePicker.getCurrentHour());
        state.putInt(START_MINUTE, mTimePicker.getCurrentMinute());
        return state;
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int hourOfDay = savedInstanceState.getInt(START_HOUR);
        int minute = savedInstanceState.getInt(START_MINUTE);
        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minute);
    }
}